package com.io.utility;

import java.io.File;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Utility implements IUtility {

	@Override
	public void printCollectionType(Collection collection) {
		System.out.println(collection.getClass().getName());	
	}

	@Override
	public Object findTheOriginal(Collection collection) {
		Iterator iterator = collection.iterator();
		Object temp; 
		Class repeatedClass = null;
		Class originalClass = null;
		
		while(iterator.hasNext()){
			temp = iterator.next();
			originalClass = temp.getClass();
			temp = iterator.next();
			if (originalClass.equals(temp.getClass())){
				repeatedClass = originalClass;
				originalClass = null;
			} else {
				originalClass = temp.getClass();
			}
		}
		return originalClass;
	}

	@Override
	public Collection makeNewCollection(Collection baseCollection) {
		Iterator iterator = baseCollection.iterator();
		Collection newCollection = new ArrayList();
		
		Object original = findTheOriginal(baseCollection);
		Object temp;
		
		while(iterator.hasNext()){
			temp = iterator.next();
			if(!temp.equals(original)){
				newCollection.add(temp);
			}
		}
		return newCollection;
	}

	@Override
	public void printMethodsFromClass(Collection collection) {
		
		try {
            Class c = Class.forName(collection.getClass().getName());
            Method m[] = c.getDeclaredMethods();
            for (int i = 0; i < m.length; i++)
            System.out.println(m[i].toString());
        }
        catch (Throwable e) {
            System.err.println(e);
        }
		
	}

	@Override
	public void serializeObjects(Collection collection, File file) {
		
		
	}

	@Override
	public void sendFileToMail(String file, String recipient) {
		 final String username = "pavelrx8@gmail.com";
		    final String password = "TYPEPASSWORD";

		    Properties props = new Properties();
		    props.put("mail.smtp.auth", true);
		    props.put("mail.smtp.starttls.enable", true);
		    props.put("mail.smtp.host", "smtp.gmail.com");
		    props.put("mail.smtp.port", "587");

		    Session session = Session.getInstance(props,
		            new javax.mail.Authenticator() {
		                protected PasswordAuthentication getPasswordAuthentication() {
		                    return new PasswordAuthentication(username, password);
		                }
		            });

		    try {

		        Message message = new MimeMessage(session);
		        message.setFrom(new InternetAddress(username));
		        message.setRecipients(Message.RecipientType.TO,
		                InternetAddress.parse(recipient));
		        message.setSubject("Testing Subject");
		        message.setText("PFA");

		        MimeBodyPart messageBodyPart = new MimeBodyPart();

		        Multipart multipart = new MimeMultipart();

		        messageBodyPart = new MimeBodyPart();

		        String fileName = "serobject.ser";
		        DataSource source = new FileDataSource(file);
		        messageBodyPart.setDataHandler(new DataHandler(source));
		        messageBodyPart.setFileName(fileName);
		        multipart.addBodyPart(messageBodyPart);

		        message.setContent(multipart);

		        System.out.println("Sending");

		        Transport.send(message);

		        System.out.println("Done");

		    } catch (MessagingException e) {
		        e.printStackTrace();
		    }

	}

}
